<?php

class CommentController extends Controller
{
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/

	public function actionAdd()
	{
		if(isset($_POST['Comment']))
		{
			$model=new ListRelationsMusic;
			echo $model->add();
		}
	}

	public function actionIndex()
	{
		if(isset($_POST['Comment']))
		{
			$id = $_POST['Comment']['id'];
			$data = Comment::model()->view($id);
			$data = json_encode($data);
			echo $data;
		}
	}

	public function actionUpdate()
	{
		if(isset($_POST['Comment']))
		{
			$id = $_POST['Comment']['id'];
			echo Comment::model()->edit($id);
		}
	}

	public function actionRemove()
	{
		if(isset($_POST['Comment']))
		{
			$id = $_POST['Comment']['id'];
			echo Comment::model()->remove($id);
		}
	}


	public function viewComment($id,$type)
	{
		$data = Comment::model()->viewComment($id,$type);
		$comments = array();

		for ($i=0; $i <count($data) ; $i++)
		{
			$comments[$i] = $data[$i]->attributes;
			$comments[$i]['like'] = array(
				'quantity' => $data[$i]['like'],
				'data' => LikeController::viewLike($data[$i]['id'],'comment')
			);
			$comments[$i]['posttime'] = NotificationController::getNotificationDateTime($comments[$i]['posttime']);
			$comments[$i]['user'] = User::model()->viewIdNameAvatar($data[$i]['user_id']);
		}
		return $comments;
	}

	public function getLinkByIdComment($comment_id)
	{
		$link = '';
		$comment = Comment::model()->getLinkByIdComment($comment_id);
		if($comment != 0){
			if($comment['music_id'] == ''){
				$link = Yii::app()->request->baseUrl.'/Page/List/'.$comment['list_id'];
			}else{
				$link = Yii::app()->request->baseUrl.'/Page/Music/'.$comment['music_id'];
			}
		}
		return $link;
	}
}